package com.example.zhidao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zhidao.model.po.Collections;
import com.example.zhidao.model.po.Goods;
import com.example.zhidao.model.responseData.ResponseData;
import com.example.zhidao.service.CollectionsService;
import com.example.zhidao.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {//3

    @Autowired
    GoodsService goodsService;

    @Autowired
    CollectionsService collectionsService;



    //    查询用户是否给改博客点赞以及收藏

    @GetMapping("/isGoodAndColl")
    public ResponseData isgood(String uid,String bid){
       List<Integer> result=new ArrayList<>();

        QueryWrapper<Goods>qw=new QueryWrapper<Goods>();
        qw.eq("uid",uid);
        qw.eq("bid",bid);

        Goods goods=goodsService.getOne(qw);


        if(goods==null){
            result.add(0,0);
        }else{
            result.add(0,1);
        }

        QueryWrapper<Collections> qw1=new QueryWrapper<>();
        qw1.eq("uid",uid);
        qw1.eq("bid",bid);
        Collections collections=collectionsService.getOne(qw1);

        if(collections==null){
            result.add(1,0);
        }else{
            result.add(1,1);
        }

        return new ResponseData(result,true,"点赞与收藏",200);

    }



//    用户给文章点赞
@PostMapping("/add")
public ResponseData addgood(Goods goods){

    QueryWrapper<Goods>qw=new QueryWrapper<Goods>();
    qw.eq("uid",goods.getUid());
    qw.eq("bid",goods.getBid());

    Goods g=goodsService.getOne(qw);

    if(g!=null){
        return new ResponseData(null,false,"已经点赞",203);
    }else{
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");//自动生成uuid
        goods.setId(uuid);
        boolean i=goodsService.addgood(goods);
        if(i){
            return new ResponseData(1,true,"点赞成功",200);
        }else{
            return new ResponseData(0,false,"点赞失败",201);

        }
    }

}

//取消点赞

    @DeleteMapping("/del")
    public ResponseData delgood(Goods goods){

        QueryWrapper<Goods>qw=new QueryWrapper<Goods>();
        qw.eq("uid",goods.getUid());
        qw.eq("bid",goods.getBid());

        Goods g=goodsService.getOne(qw);

        if(g!=null){
            boolean i=goodsService.delgood(g);
            if(i){
                return new ResponseData(1,true,"取消点赞成功",200);
            }else{
                return new ResponseData(0,false,"取消点赞失败",201);

            }
        }else{
                return new ResponseData(0,false,"未点过赞",202);
        }

    }
}
